package xyz.chaobei.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.chaobei.entity.OrgUserEntity;
import xyz.chaobei.execption.NullDataException;
import xyz.chaobei.service.OrgUserService;
import xyz.chaobei.util.JsonUtil;
import xyz.chaobei.util.PasswordUtil;

import java.util.UUID;
import javax.validation.Valid;
/**
 * 前端控制器
 *
 * @author mrc
 */
@RestController
@RequestMapping("api/orgUser")
public class OrgUserController {

    @Autowired
    private OrgUserService orgUserService;

    /**
     * 默认分页
     *
     * @return
     */
    @PostMapping("/page")
    public JSONObject page() {

        Page<OrgUserEntity> entityPage = new Page<>();
        orgUserService.page(entityPage);

        return JsonUtil.getSuccessPage(entityPage);
    }

    @PostMapping("/")
    public JSONObject save(@RequestBody OrgUserEntity orgUserEntity) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username_", orgUserEntity.getUsername());
        OrgUserEntity entity = orgUserService.getOne(wrapper);

        if (null != entity) {
            throw new RuntimeException("登录账号重复");
        }

        String salt = UUID.randomUUID().toString();
        String password = PasswordUtil.encryption(orgUserEntity.getPassword(), orgUserEntity.getUsername() + salt, 2);
        orgUserEntity.setPassword(password);
        orgUserEntity.setSalt(salt);

        boolean isSave = orgUserService.save(orgUserEntity);
        return JsonUtil.getResult(isSave);
    }

    @PutMapping("/{id}")
    public JSONObject update(@RequestBody OrgUserEntity orgUserEntity, @PathVariable("id") Integer id) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username_", orgUserEntity.getUsername());
        OrgUserEntity entity = orgUserService.getOne(wrapper);

        if (entity != null && !entity.getId().equals(id)) {
            return JsonUtil.getFailMsg("账号重复，请重试");
        }

        orgUserEntity.setId(id);
        String salt = UUID.randomUUID().toString();
        String password = PasswordUtil.encryption(orgUserEntity.getPassword(), orgUserEntity.getUsername() + salt, 2);
        orgUserEntity.setPassword(password);
        orgUserEntity.setSalt(salt);

        boolean isUpdate = orgUserService.updateById(orgUserEntity);

        return JsonUtil.getResult(isUpdate);
    }

    /**
     * 假删除数据
     *
     * @return
     */
    @DeleteMapping("/{id}")
    public JSONObject delete(@PathVariable("id") Integer id) {
        boolean isUpdate = orgUserService.removeById(id);
        return JsonUtil.getResult(isUpdate);
    }

    @GetMapping("/{id}")
    public JSONObject view(@PathVariable("id") Integer id) {

        OrgUserEntity orgUserEntity = orgUserService.getById(id);
        if (null == orgUserEntity) {
            throw new NullDataException();
        }
        orgUserEntity.setPassword(null);
        orgUserEntity.setSalt(null);

        return JsonUtil.getSuccess(orgUserEntity);
    }
}
